#! /usr/bin/python

"""
	Developerd by Andrea Stagi <stagi.andrea@gmail.com>
	TwittLine - the client twitter on your terminal
	Copyright (C) 2010 Andrea Stagi

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public Licens
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from modules import twittstd
from modules import twittconsts
from modules import twittutils
from modules import twittimer
from modules.graphic import twittcolor
from modules.graphic import twittheader
from modules.apis import twittapi
from modules import twittparms
import sys
import os
if(os.name=="posix"):
	import readline
else:
	import pyreadline

def main():

    if(len(sys.argv)==2):
	    if(sys.argv[1]==twittconsts.CMD_MAN):
	    	twittparms.getHelp()
		return twittconsts.EXIT_OK

	    elif(sys.argv[1]==twittconsts.CMD_COLORS):
		twittparms.setColors()
		return twittconsts.EXIT_OK

	    elif(sys.argv[1]==twittconsts.CMD_ABOUT):
		twittparms.getLicence()
		return twittconsts.EXIT_OK

    twittparms.getColors()

    twittheader.getHeader()

    h=twittstd.home()
    api=twittapi.getApi()

    if(api==0):
	    return 0

    h.attachApi(api)

    p=twittstd.profile()
    p.attachApi(api)

    s=twittstd.search()
    s.attachApi(api)

    f=twittstd.favorites()
    f.attachApi(api)

    r=twittstd.retweets()
    r.attachApi(api)

    dm=twittstd.direct_mess()
    dm.attachApi(api)

    twittheader.getHeader()

    pg=twittstd.page()
    pg=h
    pg.load()
    pg.show()

    

    command=["","",""]
    rcommand=""  
    help=0
    i=0

    while(command!=twittconsts.CMD_QUIT or command==twittconsts.CMD_R_QUIT):
	    
	    try:
	    	rcommand = raw_input('\n>> ')
		rcommand = rcommand.split()
			
	    except:
		twittcolor.colorprintln(twittconsts.MESS_EXEPCTRLC,0)
		return 0;

	    command=["","",""]

	    for cmd in rcommand:
		if(i<2):
			command[i]=cmd
			i+=1
		else:
			command[i]+=(cmd + " ")

	    i=0

	    if(command[0]!=twittconsts.CMD_QUIT and command[0]!=twittconsts.CMD_R_QUIT):
	    	    twittheader.getHeader()
	    else:
		    return twittconsts.EXIT_OK

	    if(command[0]==twittconsts.CMD_HOME or command[0]==twittconsts.CMD_R_HOME):
		    pg=h
		    pg.load()

	    elif(command[0]==twittconsts.CMD_DIRECT or command[0]==twittconsts.CMD_R_DIRECT):

		    pg=dm
		    pg.load(command[1])

	    elif(command[0]==twittconsts.CMD_RETWEETS or command[0]==twittconsts.CMD_R_RETWEETS):

		    pg=r
		    pg.load(command[1])

	    elif(command[0]==twittconsts.CMD_PROFILE or command[0]==twittconsts.CMD_R_PROFILE):
		    pg=p
		    pg.load(command[1])

	    elif(command[0]==twittconsts.CMD_INC):
		    pg.next()

	    elif(command[0]==twittconsts.CMD_DEC):
		    pg.prev()

	    elif(command[0]==twittconsts.CMD_HELP):
		    help=1

	    elif(command[0]==twittconsts.CMD_R_HELP):
		    help=2

	    elif(command[0]==twittconsts.CMD_NOHELP):
		    help=0

	    elif(command[0]==twittconsts.CMD_WATCH or command[0]==twittconsts.CMD_R_WATCH):
		    ht=twittimer.hometimer(h)
		    ht.activate()
		    ht.start()
		    twittutils.getchar()
		    ht.stop()
	
	    elif(command[0]==twittconsts.CMD_MORE or command[0]==twittconsts.CMD_R_MORE):
		    pg.more()

	    elif(command[0]==twittconsts.CMD_LESS or command[0]==twittconsts.CMD_R_LESS):
		    pg.less()

	    elif(command[0]==twittconsts.CMD_VIEWFOLLOWING or command[0]==twittconsts.CMD_R_VIEWFOLLOWING):
		    pg=pg.getFollowing()

	    elif(command[0]==twittconsts.CMD_VIEWFOLLOWERS or command[0]==twittconsts.CMD_R_VIEWFOLLOWERS):
		    pg=pg.getFollowers()

	    elif(command[0]==twittconsts.CMD_VIEWFAVS or command[0]==twittconsts.CMD_R_VIEWFAVS):
		    pg=pg.getFavorites()
	   
	    elif(command[0]==twittconsts.CMD_UPDATE or command[0]==twittconsts.CMD_R_UPDATE):
		    pg.update(twittutils.tinyPass(command[1] + " " +command[2]))
		    pg.load()

	    elif(command[0]==twittconsts.CMD_MESS or command[0]==twittconsts.CMD_R_MESS):
		    pg.directmess(command[1],twittutils.tinyPass(command[2]))
		    pg.load()

	    elif(command[0]==twittconsts.CMD_DELETE or command[0]==twittconsts.CMD_R_DELETE):

		    id=int(command[1])
		    pg.delete(id)
		    pg.load()

	    elif(command[0]==twittconsts.CMD_SEARCH or command[0]==twittconsts.CMD_R_SEARCH):
		    pg=s
		    pg.load(query=command[1] + " " + command[2])

	    elif(command[0]==twittconsts.CMD_FOLLOW or command[0]==twittconsts.CMD_R_FOLLOW):
		    id=int(command[1])
		    pg.follow(id)

	    elif(command[0]==twittconsts.CMD_UNFOLLOW or command[0]==twittconsts.CMD_R_UNFOLLOW):
		    id=int(command[1])
		    pg.unfollow(id)

	    elif(command[0]==twittconsts.CMD_RETWEET or command[0]==twittconsts.CMD_R_RETWEET):

		    id=int(command[1])
		    pg.retweet(id)
		    pg.load()

	    elif(command[0]==twittconsts.CMD_REPLY or command[0]==twittconsts.CMD_R_REPLY):

		    id=int(command[1])
		    pg.reply(id,twittutils.tinyPass(command[2]))
		    pg.load()

	    elif(command[0]==twittconsts.CMD_FAV or command[0]==twittconsts.CMD_R_FAV):

		    id=int(command[1])
		    pg.favorite(id)
		    pg.load()

	    elif(command[0]==twittconsts.CMD_UNFAV or command[0]==twittconsts.CMD_R_UNFAV):

		    id=int(command[1])
		    pg.unfavorite(id)
		    pg.load()

	    pg.setHelp(help)
	    pg.show()

    	    continue

    ht.release()

    return twittconsts.EXIT_OK


if __name__ == "__main__":

    sys.exit(main())



